<template>
  <div class="chart_left2">
    <div class="chart_left2-title">分行业 Top10</div>
    <div id="chart_left2" />
  </div>
</template>

<script>
const echarts = require('echarts')
import Sever from '@/api/selfApi'
export default {
  props: {
    dep_code: {
      type: String,
      default: '450000-00-0000',
      require: true
    }
  },
  data() {
    return {
      option: {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // Use axis to trigger tooltip
            type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
          }
        },
        legend: {
          textStyle: {
            color: '#fff'
          }
        },
        grid: {
          left: '1%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          splitLine: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#fff' // 更改坐标轴文字颜色
            }
          }
        },
        yAxis: {
          type: 'category',
          inverse: true,
          axisLabel: {
            show: true,
            textStyle: {
              color: '#fff' // 更改坐标轴文字颜色
            },
            formatter: (params, index) => {
              return [`${params}  {a${index + 1}|${index + 1}}`].join('\n')
            },
            rich: {
              a1: {
                color: '#fff',
                backgroundColor: 'red',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a2: {
                color: '#fff',
                backgroundColor: '#0000FF',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a3: {
                color: '#fff',
                backgroundColor: '#008000',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a4: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a5: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a6: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a7: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a8: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a9: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              },
              a10: {
                color: '#fff',
                backgroundColor: '#808080',
                width: 18,
                height: 18,
                align: 'center',
                borderRadius: 4
              }
            }
          },
          data: [
            '教育事业单位',
            '科学研究事业单位',
            '勘察设计事业单位',
            '勘探事业单位',
            '文化事业单位',
            '新闻出版事业单位',
            '广播影视事业单位',
            '卫生事业单位',
            '体育事业单位',
            '农林牧水事业单位'
          ]
        },
        color: ['rgba(0, 151, 251,0.5)', 'rgba(48, 236, 166,0.8)'],
        series: [
          {
            name: '编制数',
            type: 'bar',
            barWidth: 18,
            stack: 'total',
            label: {
              show: true
            },
            emphasis: {
              focus: 'series'
            },
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
          },
          {
            name: '空编数',
            type: 'bar',
            barWidth: 18,
            stack: 'total',
            label: {
              show: true
            },
            emphasis: {
              focus: 'series'
            },
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
          }
        ]
      },
      depcode: '450000-00-0000'
    }
  },

  watch: {
    dep_code(newValue, oldValue) {
      if (newValue) {
        (this.depcode = newValue), this.getFhy()
      }
    }
  },
  destroyed() {
    window.onresize = null
  },
  mounted() {
    // this.getFhy()
  },
  methods: {
    async getFhy() {
      try {
        // 请求后台接口
        const res = await Sever.analyse.getFhy({
          // 请求参数
          dep_code: this.depcode
        })
        if (res.length == 0) {
          // 初始化data中的数值
          Object.assign(this.$data, this.$options.data())
        } else {
          this.option.yAxis.data = res[0]
          this.option.series[0].data = res[1]
          this.option.series[1].data = res[2]
        }
        this.drawPie()
      } catch (error) {
        console.log(error)
        Object.assign(this.$data, this.$options.data())
        this.drawPie()
      }
    },
    drawPie() {
      const myChartPiechart_left2 = echarts.init(
        document.getElementById('chart_left2')
      )
      myChartPiechart_left2.clear()

      myChartPiechart_left2.setOption(this.option)
      // -----------------------------------------------------------------
      // 响应式变化
      window.addEventListener(
        'resize',
        () => myChartPiechart_left2.resize(),
        false
      )
    }
  }
}
</script>
<style lang="scss" scoped>
.chart_left2 {
  width: 100%;
  height: 64%;
  box-sizing: border-box;
  margin-bottom: 20px;
  // background-color: rgba(255, 255, 255, 0.03);
  border-top: 2px solid rgba(1, 153, 209, 0.5);
  display: flex;
  flex-direction: column;

  .chart_left2-title {
    font-weight: bold;
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 18px;
    justify-content: center;
  }

  #chart_left2 {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
